import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import * as OImage from 'core/image.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o imageEdit = '';
    @o radio = '';
    @o format = '';
    @o file = {};
    @o isAvatar = false;//上传头像
    @o isCover = false;//上传封面
    @o imageLoading = false;

    @a
    closeDialog(e) {
        e && e.preventDefault();
        this.visible = false;
        this.imageEdit = '';
    }


    @a
    confirmDialog(e, ref, uploadAvatar) {
        e.preventDefault();
        this.stores.Frame.showLoading();
        setTimeout(() => {
            let url = ref.getCroppedCanvas().toDataURL((this.format ? `image/${this.format}` : 'image/jpeg'));
            if (this.isAvatar) {
                this.fire('artworkEditor:avatar', {img: url});
            } else if (this.isCover) {
                this.fire('artworkEditor:cover', {img: url});
            }
            this.closeDialog();
            this.stores.Frame.hideLoading();
        }, 20);

    }

    @a
    resetEditor(e, ref) {
        e.preventDefault();
        ref.reset();
    }

    @a
    reUpload(e, ref) {
        if (this.uploadAvatar) {
            this.fire('user:reUpload', {e, ref});
        } else if (this.uploadCover) {
            this.fire('exhibitionInfo:reUpload', {e, ref});
        } else if (this.uploadVideoCover) {
            this.fire('artworkInfo:reUpload', {e, ref});
        } else if (this.isPCUserCover) {
            this.fire('pcCover:reUpload', {e, ref});
        } else if (this.uploadUserCover) {
            this.fire('phoneCover:reUpload', {e, ref});
        } else if (this.uploadArtistAvatar && this.isInsertArtist) {
            this.fire('artistsUpload:reUpload', {e, ref, index: this.index});
        } else if (this.uploadArtistAvatar) {
            this.fire('artistsInfo:reUpload', {e, ref});
        } else if (this.uploadVideoUploadCover) {
            this.fire('artworkForUpload:reUpload', {e, ref});
        } else if (this.isAvatar){
            this.fire('usersEdit:reUpload', {e, ref});
        }
    }


    will() {

    }

    mount(ref) {
        this.on('artworkEditor:show', (data) => {
            this.visible = true;
            this.isCover = false;
            this.isAvatar = false;
            this.imageEdit = '';
            this.format = '';
            this.file = {};
            this.radio = '';
            this.format = data.fileFormat === 'jpg' ? 'jpeg' : data.fileFormat;
            this.radio = data.radio || '';
            this.file = data.file || {};
            this.imageLoading = true;
            setTimeout(() => {
                OImage.compressImgNormal(this.file, data.thumb, (res) => {
                    this.imageEdit = res;
                    this.imageLoading = false;
                }, () => {
                    this.imageEdit = data.thumb;
                    this.imageLoading = false;
                });
            }, 20);

            if (data.isCover) {
                this.isCover = true;
            }
            if (data.isAvatar) {
                this.isAvatar = true;
            }

        });

    }

}

export default Store;